package com.atguigu.flink03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/2/20
 * 该案例演示了使用数据生成器生成数据
 */
public class Flink06_Source_DataGen {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        //TODO 2.读取数据数据生成器生成的数据 封装为流
        DataGeneratorSource<String> dataSource = new DataGeneratorSource<String>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long value) throws Exception {
                        return "数据:" + value;
                    }
                },
                100,
                RateLimiterStrategy.perSecond(10),
                //TypeInformation.of(String.class)
                Types.STRING
        );
        DataStreamSource<String> dataDS = env.fromSource(
                dataSource,
                WatermarkStrategy.noWatermarks(),
                "data_source"
        );
        dataDS.print();
        env.execute();
    }
}
